# -*- coding: utf-8 -*-
# created on 2018/05/16

from mathsolver.functions.base import *
from sympy import Intersection, Union


class GongChaRangeUpdate001(BaseFunction):
    """
    设{a_{n}}是公差为正数的等差数列,若a_{1}+a_{2}+a_{3}=15,a_{1}a_{2}a_{3}=80,则a_{11}+a_{12}+a_{13}=().
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseSequence)
        assert isinstance(args[1], str)
        sl = self.search(args[0].name)
        text = args[1]
        if text.find("正") >= 0:
            sl.dRange = Intersection(Interval(0, S.Infinity, True, True), sl.dRange)
        else:
            raise Exception("to do")
        return self


class GongChaRangeUpdate002(BaseFunction):
    """
    已知等差数列{a_{n}}的公差不为零,a_{1}=25,a_{1},a_{11},a_{13}成等比数列.求{a_{n}}的通项公式.
    """
    def solver(self, *args):
        assert len(args) == 3
        assert isinstance(args[0], BaseSequence)
        assert isinstance(args[1], str)
        sl = self.search(args[0].name)
        text = args[1]
        value = args[2].sympify()
        if text.find('不') >= 0:
            d_range = Union(Interval(value, S.Infinity, True, True), Interval(- S.Infinity, value, True, True))
            sl.dRange = Intersection(d_range, sl.dRange)
        else:
            raise Exception("to do")
        return self


class GongChaRangeUpdate003(BaseFunction):
    """
    已知递增的等差数列{a_{n}}满足a_{1}=1,a_{3}={a_{2}}^2-4,则a_{n}=.
    """
    def solver(self, *args):
        assert len(args) == 2
        seq = args[0].sympify()
        text = args[1]
        p = r'[a-zA-Z]_\(.*?\)'
        m = re.findall(p, str(seq))
        m = set(m)
        m = list(m)
        assert len(m) == 1
        new_item = sympify(m[0])
        new_item_name = new_item.func
        sl = self.search(str(new_item_name))
        if text.find("增") >= 0:
            d_range = Interval(0, S.Infinity, True, True)
            sl.dRange = Intersection(d_range, sl.dRange)
        else:
            raise Exception("to do")
        self.output.append(sl)
        return self


class GongChaRangeUpdate004(BaseFunction):
    """
    等差数列{a_{n}}中,a_{1}=23,公差d为整数,若a_{6}>0,a_{7}<0.求公差d的值.
    """
    def solver(self, *args):
        assert len(args) == 2
        shulies = [self.known[item] for item in self.known if isinstance(self.known[item], BaseShuLieDengCha)]
        assert len(shulies) == 1
        shulie = shulies[0]
        shulie.dValue = args[0].sympify()
        text = args[1]
        if text.find("整数") >= 0:
            shulie.dRange = range(1, 10000)
        return self


class GongChaRangeUpdate(BaseFunction):
    CLS = [GongChaRangeUpdate001, GongChaRangeUpdate002, GongChaRangeUpdate003, GongChaRangeUpdate004]

    def solver(self, *args):
        r = None
        for cl in GongChaRangeUpdate.CLS:
            try:
                r = cl(known=self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
